#include "config.h"
#include "util.h"
#include "sched_util.h"
#include "sched_msgs.h"
#include "validate_util.h"
#include "error_numbers.h"
#include "ddas_datarow.h"
#include "ddas_validator_helpers.h"
#include "ddas_definitions.h"

using std::vector;
using std::string;

int init_result(RESULT const & result, void*& data)
{
	//boinc variables
	FILE* f;
        string path;
	int retval;
	
	//discover the location of the result's output file
        retval = get_output_file_path(result, path);
        if (retval) 
	{
        	log_messages.printf
		(
            		SCHED_MSG_LOG::MSG_CRITICAL,
            		"[RESULT#%d %s] check_set: can't get output filename\n",
            		result.id, 
			result.name
        	);

		return retval;
	}
        
	//read result into *f
        retval = try_fopen(path.c_str(), f, "r");
        if (retval) 
	{
        	log_messages.printf
		(
            		SCHED_MSG_LOG::MSG_CRITICAL,
            		"[RESULT#%d %s] Couldn't open %s\n",
            		result.id, 
			result.name,
			path.c_str()
        	);
	}

	char* xml=filePointerToCharPointer(f);
	char** defNums=getDefNumbers(xml);
        
	data = (void*) defNums;
	return 0; 
}

int compare_results(RESULT & r1, void* data1, RESULT const& r2, void* data2, bool& match)
{
	char** charData1=(char**)data1;
	char** charData2=(char**)data2;

        match=true;
        for(int i=0; i<EXPECTED_NUMBER_OF_DATAROWS; i++)
        {
                for(int j=0; j<EXPECTED_NUMBER_OF_DATAROWS; j++)
                {
                        //found a match so we can iterate 'i'
                        if(strcmp(charData1[i], charData2[j])==0) break;

			//failure to match condition
                        if(j==EXPECTED_NUMBER_OF_DATAROWS-1)
			{
				match=false;
				return 0;
			}
                }
        }

	return 1;
}

int cleanup_result(RESULT const&, void* data1)
{
	if(data1!=NULL)
	free((char**)data1);
	return 0;
}

double compute_granted_credit(vector<RESULT>& results)
{
	return (double)(results.size()*CREDIT_PER_RESULT); 
}


//HELPER CODE

int getFileSize(FILE* fp)
{
        int bytesRead=0;
        while(fgetc(fp)!=EOF)
        {
                bytesRead++;
        }

        rewind(fp);
        return bytesRead;
}

char* filePointerToCharPointer(FILE* fp)
{
        char* fileData=(char*)malloc(getFileSize(fp));
        int index=0;
        int c;

        while((c=fgetc(fp))!=EOF)
        {
                fileData[index++]=c;
        }

        rewind(fp);
        return fileData;
}

char** getDefNumbers(char* xml)
{
        char* space=" ";
        char* remainingXML=(char*)malloc(strlen(xml));
        char* curString=(char*)malloc(strlen(xml));
        char** defNumbers=(char**)malloc(EXPECTED_NUMBER_OF_DATAROWS);

        int index=0;
        while((remainingXML=strstr(xml,"defNumber="))!=NULL)
        {
                strcpy(xml,remainingXML+strlen("defNumber="));
                curString=mystrtok(xml,space);

                defNumbers[index++]=curString;
                //puts(curString);
                //puts(xml);
        }

        return defNumbers;
}

char* mystrtok(char* string, char* delim)
{
        int strLen=strlen(string);
        char* result=(char*)malloc(strlen(string));
        char* space=" ";

        for(int i=0; i<strLen; i++)
        {
                if(string[i]==*space)
                {
                        //puts("MATCH FOUND");
                        return result;
                }

                result[i]=string[i];
        }

        //puts("NO MATCHES");
        return "NO MATCHES";
}

